package com.mezizy.ifaner;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mezizy.ifaner.global.Config;
import com.mezizy.ifaner.global.Global;
import com.mezizy.ifaner.global.com.mezizy.ifaner.web.data.User;


public class SelfInformationTab extends Fragment {
    static View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.tab_self_information, container, false);



        view.findViewById(R.id.btnClearLogInInformation).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 首先获取一个SharedPreferences对象
                SharedPreferences settings =getActivity(). getSharedPreferences(Config.SHARED_PREFERENCE, 0);
                // 将用户名和密码保存进去
                SharedPreferences.Editor editor = settings.edit();
                editor.putString( Config.SHARED_ID, "");
                editor.putString( Config.SHARED_PWD_SHA, "");
                editor.commit();

                Global.userCredit=0;
                Global.userName=null;
                Global.userId=null;
                Global.userPwdSha=null;

                getActivity().startActivity(new Intent(getActivity(),LogInActivity.class));
            }
        });


        view.findViewById(R.id.btnAboutUs).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 new AlertDialog.Builder(getActivity())
                         .setTitle("愛飯兒 iFaner Version 0.9")
                         .setMessage("\r\n愛飯開發團隊联系邮箱:\r\nmezizy@live.com\r\n")
                         .show();
            }
        });

        view.findViewById(R.id.cardViewLogOut).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        ((CardView) v).setCardBackgroundColor(getResources().getColor(R.color.button_red_dark));

                        break;

                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP:
                        ((CardView) v).setCardBackgroundColor(getResources().getColor(R.color.button_red));
                        break;
                }
                return false;
            }
        });

        view.findViewById(R.id.cardViewLogOut).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Global.userCredit=0;
                Global.userName=null;
                Global.userId=null;
                Global.userPwdSha=null;

                getActivity().startActivity(new Intent(getActivity(),LogInActivity.class).putExtra("isLogOut",true));
            }
        });


        try{

            ((TextView) view.findViewById(R.id.txtUserId)).setText(Global.userId);
            ((TextView) view.findViewById(R.id.txtUserName)).setText(Global.userName);
            ((TextView) view.findViewById(R.id.txtCredit)).setText(""+Global.userCredit);

        }catch (Exception e){
            ((TextView) view.findViewById(R.id.txtUserId)).setText(Global.userId);
            ((TextView) view.findViewById(R.id.txtUserName)).setText("");
            ((TextView) view.findViewById(R.id.txtCredit)).setText("");
        }
        return view;

    }


    public static void startGetUserInf() {
        new GetUserInfAsync().execute();
    }
    static class GetUserInfAsync extends AsyncTask<User,User,User>{
        @Override
        protected void onPostExecute(User user) {
            Global.userName=user.getUserName();
            Global.userCredit=user.getUserCredit();
            try{
                ((TextView) view.findViewById(R.id.txtUserId)).setText(Global.userId);
                ((TextView) view.findViewById(R.id.txtUserName)).setText(Global.userName);
                ((TextView) view.findViewById(R.id.txtCredit)).setText(""+Global.userCredit);
            }catch (Exception e){
                e.printStackTrace();
                ((TextView) view.findViewById(R.id.txtUserId)).setText(Global.userId);
                ((TextView) view.findViewById(R.id.txtUserName)).setText("");
                ((TextView) view.findViewById(R.id.txtCredit)).setText("");
            }
            super.onPostExecute(user);
        }

        @Override
        protected User doInBackground(User... params) {

            return Global.getUserInf();
        }
    }
}
